package com.sls.web.manager.recharge.service.impl;

import java.util.UUID;

import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.core.socket.TransmissionCode;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.pojo.UserInfoVO;
import com.sls.web.manager.recharge.pojo.Supplier;
import com.sls.web.manager.recharge.querybean.SupplierQuery;
import com.sls.web.manager.recharge.service.SupplierService;
import com.sls.web.manager.socket.SuperService;

@Service("supplierService")
public class SupplierServiceImpl extends SuperService implements
		SupplierService {
	@Override
	public boolean addSupplier(Supplier supplier, UserInfoVO userinfo)
			throws Exception {
		String ip = userinfo.getCurrentIp();
		Head head = new Head();
		head.setIp(ip);
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.INSERT_SUPPLIER);
		head.setUserId(userinfo.getUserId());
		SocketDTO dto = super.send(head, supplier);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode()))
			return true;
		else
			throw new ManagerException(dto.getHead().getResponseMessage());

	}

	@Override
	public Pagination<?> querySupplier(SupplierQuery rQuery, UserInfoVO userinfo)
			throws Exception {
		Gson gson = CommonUtil.gson();
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.QUERY_SUPPLIER_LIST);
		head.setIp(userinfo.getCurrentIp());
		head.setUserId(userinfo.getUserId());
		SocketDTO socketDTO = super.send(head, rQuery);
		String body = socketDTO.getBody();
		Pagination<Supplier> supplier = gson.fromJson(body,
				new TypeToken<Pagination<Supplier>>() {
				}.getType());

		return supplier;
	}

	@Override
	public boolean delete(String id, UserInfoVO userinfo) throws Exception {
		String ip = userinfo.getCurrentIp();
		Head head = new Head();
		head.setIp(ip);
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.DELETE_SUPPLIER);
		Supplier su = new Supplier();
		su.setId(id);
		head.setUserId(userinfo.getUserId());
		SocketDTO dto = super.send(head, su);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode()))
			return true;
		else
			throw new ManagerException(dto.getHead().getResponseMessage());
	}

	@Override
	public Supplier querySupplierById(String id, UserInfoVO userinfo)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.QUERY_SUPPLIER_BY_ID);
		head.setIp(userinfo.getCurrentIp());
		head.setUserId(userinfo.getUserId());
		SocketDTO dto = new SocketDTO();
		Supplier su = new Supplier();
		su.setId(id);
		dto = super.send(head, su);
		Supplier supplier = CommonUtil.gson().fromJson(dto.getBody(),
				Supplier.class);
		return supplier;
	}

	@Override
	public boolean updateSupplier(Supplier supplier, UserInfoVO userinfo)
			throws Exception {
		String ip = userinfo.getCurrentIp();
		Head head = new Head();
		head.setIp(ip);
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.UPDATE_SUPPLIER);
		head.setUserId(userinfo.getUserId());
		SocketDTO dto = super.send(head, supplier);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode()))
			return true;
		else
			throw new ManagerException(dto.getHead().getResponseMessage());
	}

	@Override
	public boolean checkName(Supplier supplier, UserInfoVO userinfo) {
		String ip = userinfo.getCurrentIp();
		Head head = new Head();
		head.setIp(ip);
		head.setServiceCode(Constants.CHECK_SUPPLIER_INTERFACE_NAME);
		head.setUserId(userinfo.getUserId());
		supplier.setInterfaceName(supplier.getInterfaceName());
		supplier.setDeleted(false);
		SocketDTO dto = super.send(head, supplier);
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode()))
			return true;
		else
			return false;
	}
}
